package com.yuexunit.finance.system.action.v1.poi;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;  

public class PoiDocUtil {
	 
    //private static final Logger logger = Logger.getLogger(XwpfUtil.class);  
  
    /** 
     * ����word�ļ� 
     * @param params 
     * @param is 
     * @param request 
     * @param response 
     * @param xwpfUtil 
     */  
//    public void exportWord(Map<String, Object> params, InputStream is,  
//            HttpServletRequest request, HttpServletResponse response,  
//            XwpfUtil xwpfUtil) {  
          
//        XWPFDocument doc = new XWPFDocument();  
//        xwpfUtil.replaceInPara(doc,params);  
//        xwpfUtil.replaceInTable(doc,params);  
//        try {  
//            OutputStream os = response.getOutputStream();  
//            response.setContentType("application/vnd.ms-excel");  
//            response.setHeader("Content-disposition","attachment;filename=exportWord.docx");//�ļ������Ĳ���ʾ  
//            //�ѹ���õ��ĵ�д�������  
//            doc.write(os);  
//            //�ر���  
//            xwpfUtil.close(os);  
//            xwpfUtil.close(is);  
//            os.flush();  
//            os.close();  
//        } catch (IOException e) {  
//            logger.error("�ļ���������");  
//        }  
//    }  

	/** 
	    * �滻��������ı��� 
	    * @param doc Ҫ�滻���ĵ� 
	    * @param params ���� 
	    */  
	   public void replaceInPara(XWPFDocument doc, Map<String, Object> params) {  
	      Iterator<XWPFParagraph> iterator = doc.getParagraphsIterator();  
	      XWPFParagraph para;  
	      while (iterator.hasNext()) {  
	         para = iterator.next();  
	         this.replaceInPara(para, params);  
	      }  
	   }  
	    
	   /** 
	    * �滻��������ı��� 
	    * @param para Ҫ�滻�Ķ��� 
	    * @param params ���� 
	    */  
	   public void replaceInPara(XWPFParagraph para, Map<String, Object> params) {  
	      List<XWPFRun> runs;  
	      Matcher matcher;  
	      if (this.matcher(para.getParagraphText()).find()) {  
	         runs = para.getRuns();  
	         for (int i=0; i<runs.size(); i++) {  
	            XWPFRun run = runs.get(i);  
	            String runText = run.toString();  
	            matcher = this.matcher(runText);  
	            if (matcher.find()) {  
	                while ((matcher = this.matcher(runText)).find()) {  
	                   runText = matcher.replaceFirst(String.valueOf(params.get(matcher.group(1))));  
	                }  
	                //ֱ�ӵ���XWPFRun��setText()���������ı�ʱ���ڵײ�����´���һ��XWPFRun�����ı������ڵ�ǰ�ı����棬  
	                //�������ǲ���ֱ����ֵ����Ҫ��ɾ����ǰrun,Ȼ�����Լ��ֶ�����һ���µ�run��  
	                para.removeRun(i);  
	                para.insertNewRun(i).setText(runText);  
	            }  
	         }  
	      }  
	   }  
	    
	   /** 
	    * �滻�������ı��� 
	    * @param doc Ҫ�滻���ĵ� 
	    * @param params ���� 
	    */  
	   public void replaceInTable(XWPFDocument doc, Map<String, Object> params) {  
	      Iterator<XWPFTable> iterator = doc.getTablesIterator();  
	      XWPFTable table;  
	      List<XWPFTableRow> rows;  
	      List<XWPFTableCell> cells;  
	      List<XWPFParagraph> paras;  
	      while (iterator.hasNext()) {  
	         table = iterator.next();  
	         rows = table.getRows();  
	         for (XWPFTableRow row : rows) {  
	            cells = row.getTableCells();  
	            for (XWPFTableCell cell : cells) {  
	                paras = cell.getParagraphs();  
	                for (XWPFParagraph para : paras) {  
	                   this.replaceInPara(para, params);  
	                }  
	            }  
	         }  
	      }  
	   }  
	    
	   /** 
	    * ����ƥ���ַ��� 
	    * @param str 
	    * @return 
	    */  
	   private Matcher matcher(String str) {  
	      Pattern pattern = Pattern.compile("\\{(.+?)\\}", Pattern.CASE_INSENSITIVE);  
	      Matcher matcher = pattern.matcher(str);  
	      return matcher;  
	   }  
	    
	   /** 
	    * �ر������� 
	    * @param is 
	    */  
	   public void close(InputStream is) {  
	      if (is != null) {  
	         try {  
	            is.close();  
	         } catch (IOException e) {  
	            e.printStackTrace();  
	         }  
	      }  
	   }  
	    
	   /** 
	    * �ر������ 
	    * @param os 
	    */  
	   private void close(OutputStream os) {  
	      if (os != null) {  
	         try {  
	            os.close();  
	         } catch (IOException e) {  
	            e.printStackTrace();  
	         }  
	      }  
	   }  

}
